/* eslint-disable no-undef,no-param-reassign */
import React, { Component } from 'react';
import { Form, Icon, Input, Button, Select, message } from 'antd';
import styles from './Login.css';
import * as commonConfig from '../../utils/config';
// import * as commonUtils from '../../utils/utils';
import * as commonServices from '../../services/services';
import * as commonFunc from '../Common/commonFunc';
import logo from '../../assets/footer_logo.png';

const FormItem = Form.Item;
const { Option } = Select;

class LoginForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      companys: [],
      sParentId: '',
      sId: '',
    };
  }

  async componentWillMount() {
    const configUrl = `${commonConfig.server_host}sysbrands/getSysbrands`;
    const configReturn = (await commonServices.getService(null, configUrl)).data;
    if (configReturn.code === 1) {
      const companys = configReturn.dataset.rows;
      this.setState({ companys });
    } else {
      message.error(configReturn.msg);
    }
  }

  handleSelect = (value, option) => {
    const sParentId = option.props['data-parentid'];
    this.setState({
      sParentId,
      sId: value,
    });
  };

  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields(async (err, values) => {
      if (!err) {
        const value = {};
        value.username = values.username;
        value.password = values.password;
        value.sParentId = this.state.sParentId;
        value.sId = this.state.sId;
        const url = `${commonConfig.server_host}userlogin/${value.sParentId}/${value.sId}`;
        const dataReturn = (await commonServices.postValueService(null, value, url)).data;
        if (dataReturn.code === 1) {
          const {
            token, gdslogininfo: userinfo, systemData, commonConst,
          } = dataReturn.dataset.rows[0];
          // commonUtils.setSystemSettings(systemData);
          // commonUtils.setCommonConst(commonConst);
          const decimals = {};
          if (systemData !== undefined) {
            const dNetPrice = systemData.filter(item => (item.sName === 'NetPrice'))[0];
            const dNetMoney = systemData.filter(item => (item.sName === 'NetMoney'))[0];
            decimals.dNetPrice = dNetPrice.sValue !== undefined ? (dNetPrice.sValue) * 1 : 6;
            decimals.dNetMoney = dNetMoney.sValue !== undefined ? (dNetMoney.sValue) * 1 : 6;
            // commonUtils.setNetPrice(decimals.dNetPrice);
            // commonUtils.setNetMoney(decimals.dNetMoney);
            // commonUtils.setDateFormat(commonFunc.getDateFormat(systemData));
          }
          this.props.onLogin({
            token, systemData, commonConst, userinfo, decimals, dateFormat: commonFunc.getDateFormat(systemData),
          });
        } else {
          message.error(dataReturn.msg);
        }
      }
    });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { companys } = this.state;
    const options = companys.map(com =>
      (
        <Option key={com.sId} data-parentid={com.sParentId} className={styles.loginOption}>
          {com.sName}
        </Option>));
    const { loginLoading } = this.props.login;
    return (
      <div className={styles.wraper}>
        <div className={styles.header}>
          <div className={styles.logo}>
            <h1>小羚羊<span>欢迎登录ERP</span></h1>
          </div>
        </div>
        <div className={styles.login}>
          <div className={styles.loginFormCenter}>
            <div className={styles.leftCon}>
              <h1>上海小羚羊软件</h1>
              <h2>智能印刷全流程</h2>
              <h3>ERP</h3>
            </div>
            <Form onSubmit={this.handleSubmit} className={styles.loginForm}>
              <div className={styles.loginTitle}>
                用户登录
              </div>
              <div className={styles.loginMiddle}>
                <FormItem className={styles.loginTip}>
                  {getFieldDecorator('username', {
                    rules: [{ required: true, message: '请输入你的用户名!' }],
                  })(<Input className={styles.loginInput} prefix={<Icon type="user" className={styles.loginIcon} />} placeholder="请输入用户名" />)}
                </FormItem>
                <FormItem className={styles.loginTip}>
                  {getFieldDecorator('password', {
                    rules: [{ required: true, message: '请输入你的密码!' }],
                  })(<Input className={styles.loginInput} prefix={<Icon type="lock" className={styles.loginIcon} />} type="password" placeholder="请输入密码" />)}
                </FormItem>
                <FormItem className={styles.loginTip}>
                  <div className={styles.loginSelect}>
                    {getFieldDecorator('companys', {
                      rules: [{ required: true, message: '请选择公司名!' }],
                    })(<Select placeholder="请选择公司名" onSelect={this.handleSelect}>{options}</Select>)}
                  </div>
                </FormItem>
              </div>
              <FormItem className={styles.loginBottom}>
                <Button type="primary" htmlType="submit" className={styles.loginButton} loading={loginLoading}>
                  登 录
                </Button>
              </FormItem>
            </Form>
          </div>
        </div>
        <div className={styles.footer}>
          <img alt="logo" src={logo} />
          ©2014-2018 上海小羚羊软件股份 | xlyprint.com | 印刷电商平台  文件智能处理   自动拼版   印刷ERP   400-880-6237
        </div>
      </div>
    );
  }
}

const LoginFormComponent = Form.create()(LoginForm);

export default LoginFormComponent;
